<?php 
namespace app\admin\controller;
use think\Controller;
/**
* 商品管理相关
*/
class Product extends Controller
{
	public function index()
	{
		return 'Product';
	}
	/**
	 * [分类管理]
	 * @Author   lessant@126.com
	 * @DateTime 2017-11-05T11:10:46+0800
	 * @return   [type]                   [description]
	 */
	public function product_category()
	{
		
		return $this->fetch();
	}
	/**
	 * [显示分类信息]
	 * @Author   lessant@126.com
	 * @DateTime 2017-11-06T15:41:22+0800
	 * @return   [type]                   [description]
	 */
	public function product_category_ajax()
	{	
		$c_data = db('category')->field('id,cname name,pid')->select();
		$c_data[0]['open'] = 'true';			//展开顶级分类
		echo json_encode($c_data,JSON_UNESCAPED_UNICODE);
	}
	/**
	 * [添加分类界面]
	 * @Author   lessant@126.com
	 * @DateTime 2017-11-06T14:54:01+0800
	 * @return   [type]                   [description]
	 */
	public function product_category_add_show()
	{
		// $c_data = db('product_category')->select();
		// foreach ($c_data as $k => $v) {
		// 		$c_data[$k]['cname'] = str_repeat($c_data[0]['cname'].'-->', $v['level']).$v['name'];
		// }
		// $this->assign('c_data',$c_data);
		return $this->fetch();
	}
	/**
	 * [添加分类]
	 * @Author   lessant@126.com
	 * @DateTime 2017-11-05T11:20:41+0800
	 * @return   [type]                   [description]
	 */
	public function product_category_add()
	{	
		$data = input('post.');
		$msg = '';
		if( !$data['cname'] ){
			return ['error'=>'1','msg'=>'请填写分类名称'];
		}
		if( !$data['pid'] ){
			return ['error'=>'1','msg'=>'请选择父级类别'];
		}
		$db = db('product_category');
		$is_exist = $db->where('cname',$data['cname'])->find();
		if( $is_exist ){
			return ['error'=>'1','msg'=>'该分类已存在'];
		}
		$data['level'] = 2;
		$data['path'] = '0,1';
		$res = $db->insertGetId($data);			//添加成功返回自增id
		$new_path = '0,1'.$res;
		$res = $db->where('id',$res)->update(['path'=>'0,1,'.$res]);
		if( $res ){
			return ['error'=>'0','msg'=>'添加成功','url'=>url('admin/product/product_category')];
		}else{
			return ['error'=>'1','msg'=>'添加失败'];
		}
	}
	/**
	 * [修改商品分类界面]
	 * @Author   lessant@126.com
	 * @DateTime 2017-11-07T09:38:31+0800
	 * @return   [type]                   [description]
	 */
	public function category_edit_show()
	{	
		$cname = input('get.cname');
		$this->assign('cname',$cname);
		return $this->fetch();
	}
	/**
	 * [修改商品分类]
	 * @Author   lessant@126.com
	 * @DateTime 2017-11-07T09:39:45+0800
	 * @return   [type]                   [description]
	 */
	public function product_category_edit()
	{
		$cname = input('post.cname');
		$odl_cname = input('post.oldcname');
		if( !$cname || !$odl_cname ){
			return ['error'=>'1','msg'=>'请填写分类名称'];
		}
		$res = db('product_category')->where('cname',$odl_cname)->find();
		if( $res ){
			return ['error'=>'1','msg'=>'该分类已存在'];		//该分类已存在
		}
		$res = db('product_category')->where('cname',$odl_cname)->update(['cname'=>$cname]);
		if( $res ){
			return ['error'=>'0','msg'=>'修改成功'];			//修改成功
		}else{
			return ['error'=>'1','msg'=>'修改失败'];			//修改失败
		}
	}
	/**
	 * [删除分类]
	 * @Author   lessant@126.com
	 * @DateTime 2017-11-06T23:15:34+0800
	 * @return   [type]                   [description]
	 */
	public function product_category_del()
	{
		$cname = input('post.cname');
		$res = db('product_category')->where('cname',$cname)->delete();
		if( $res ){
			return ['error'=>'0'];								//删除成功
		}else{
			return ['error'=>'1'];								//删除失败
		}
	}
	/**
	 * [商品属性管理]
	 * @Author   lessant@126.com
	 * @DateTime 2017-11-07T15:54:27+0800
	 * @return   [type]                   [description]
	 */
	public function product_attr()
	{
		$c_data = db('color')->select();
		$this->assign('c_data',$c_data);
		return $this->fetch();
	}
	/**
	 * [添加颜色]
	 * @Author   lessant@126.com
	 * @DateTime 2017-11-07T20:26:23+0800
	 * @return   [type]                   [description]
	 */
	public function product_color_add()
	{
		$color = input('post.color');
		$res = db('color')->where('color',$color)->find();
		if( $res ){
			return ['error'=>'1','msg'=>'该颜色已存在'];
		}
		$res = db('color')->insertGetId(['color'=>$color]);		//添加成功返回自增id
		if( $res ){
			return ['error'=>'0','msg'=>'添加成功'];			//添加成功
		}else{
			return ['error'=>'1','msg'=>'添加失败'];			//添加失败
		}
	}
	/**
	 * [颜色编辑界面]
	 * @Author   lessant@126.com
	 * @DateTime 2017-11-07T20:58:41+0800
	 * @return   [type]                   [description]
	 */
	public function color_edit_show()
	{	
		$cname = input('get.cname');
		$this->assign('cname',$cname);
		return 	$this->fetch();	
	}
	/**
	 * [颜色编辑]
	 * @Author   lessant@126.com
	 * @DateTime 2017-11-07T21:11:07+0800
	 * @return   [type]                   [description]
	 */
	public function color_edit()
	{	
		//超过20个文字会报错!!!
		$cname = input('post.cname');
		$odl_cname = input('post.oldcname');
		if( !$cname || !$odl_cname){
			return ['error'=>'1','msg'=>'请填写颜色名称'];
		}if( $cname == $odl_cname){
			return ['error'=>'0','msg'=>'修改成功'];
		}
		$res = db('color')->where('color',$cname)->find();
		if( $res ){
			return ['error'=>'1','msg'=>'该颜色已存在'];		//该颜色已存在
		}
		$res = db('color')->where('color',$odl_cname)->update(['color'=>$cname]);
		if( $res ){
			return ['error'=>'0','msg'=>'修改成功'];			//修改成功
		}else{
			return ['error'=>'1','msg'=>'修改失败'];			//修改失败
		}
	}
	/**
	 * [删除颜色]
	 * @Author   lessant@126.com
	 * @DateTime 2017-11-07T21:28:37+0800
	 * @return   [type]                   [description]
	 */
	public function color_del()
	{
		$cname = input('post.cname');
		$res = db('color')->where('color',$cname)->delete();
		if( $res ){
			return ['error'=>'0'];								//删除成功
		}else{
			return ['error'=>'1'];								//删除失败
		}
	}
	/**
	 * [商品列表]
	 * @Author   lessant@126.com
	 * @DateTime 2017-11-05T11:21:14+0800
	 * @return   [type]                   [description]
	 */
	public function product_list()
	{	
		return $this->fetch();
	}
	/**
	 * [商品添加界面]
	 * @Author   lessant@126.com
	 * @DateTime 2017-11-05T11:21:14+0800
	 * @return   [type]                   [description]
	 */
	public function product_add_show()
	{	
		$category = db('category')->field('id,cname')->select();
		// var_dump($category);
		$this->assign('category',$category);
		return $this->fetch();
	}
	/**
	 * [添加商品]
	 * @Author   lessant@126.com
	 * @DateTime 2017-11-08T20:15:39+0800
	 * @return   [type]                   [description]
	 */
	public function product_add()
	{
		$data = input('post.');
		var_dump($data);
	}
	/**
	 * [商品预览]
	 * @Author   lessant@126.com
	 * @DateTime 2017-11-05T11:38:17+0800
	 * @return   [type]                   [description]
	 */
	public function product_show()
	{
		return $this->fetch();
	}
}



 ?>